<template>
  <div class="dashboard-container">
    <div class="dashboard-text">姓名: {{ updateForm.username }}</div>
    <div class="dashboard-text">手机号: {{ updateForm.phone }}</div>
    <div class="dashboard-text">上次登录时间: {{ updateForm.lastlogindatetime }}</div>
    <div class="dashboard-text">角色: {{ updateForm.identity === 0 ? '学生' : '老师' }}</div>
    <el-button type="text" @click="dialogVisible = true">修改个人信息</el-button>
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose">
      <el-form  label-width="80px" :model="userForm">
        <el-form-item label="姓名">
          <el-input v-model="userForm.username"></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="userForm.phone" disabled></el-input>
        </el-form-item>
        <el-form-item label="真实姓名">
          <el-input v-model="userForm.truename"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="reload()">取 消</el-button>
    <el-button type="primary" @click="updateInfo()">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>

export default {
  name: 'Dashboard',
  data() {
    return {
      dialogVisible: false,
      userInfo: '',
      updateForm: '',
      userForm: ''
    }
  },
  created() {
    this.getUserInfo()
  },
  mounted() {
  },
  methods: {
    reload() {
      this.getUserInfo()
      this.dialogVisible = false
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          this.reload()
        })
        .catch(_ => {})
    },
    async getUserInfo() {
      const [err, res] = await this.$request('/user/userinfo')
      if (err) {
        return this.$message.error(err.data.msg)
      }

      // 将返回的用户信息保存至localstorage中
      console.log(res)
      this.updateForm = res.result
      this.userForm = res.result
    },
    async updateInfo() {
      const form = { ...this.userForm }
      const [err, res] = await this.$request('http://localhost:8081/user/alert',
        { ...form },
        'post',
        'params')
      if (err) {
        this.dialogVisible = false
        return this.$message.error(err.data.msg)
      }
      if (res.code === 200) {
        localStorage.setItem('userInfo', JSON.stringify(form))
        this.$message.success('修改成功')
        this.$router.go(0)
      }
      this.dialogVisible = false
    }
  }
}
</script>

<style lang="scss" scoped>
.dashboard {
  &-container {
    margin: 30px;
  }

  &-text {
    font-size: 30px;
    line-height: 46px;
  }
}
</style>
